// leaderPerson/pages/leaderClosed/leaderClosed.js
var common = require("../../../common/common.js").common;

var dateTimePicker = require("./dateTimePicker.js");

Page({
    data: {
        date: "2018-10-01",
        time: "12:00",
        dateTimeArray: null,
        dateTime: null,
        dateTimeArray1: null,
        dateTime1: null,
        startYear: 2020,
        endYear: 2050,
        tmplIds: [],
        //模板消息tmplIds
        subscribeMessage: true
    },
    onLoad(options) {
        // 获取完整的年月日 时分秒，以及默认显示的数组
        var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
        var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
        // 精确到分的处理，将数组的秒去掉
                var lastArray = obj1.dateTimeArray.pop();
        var lastTime = obj1.dateTime.pop();
        this.setData({
            dateTime: obj.dateTime,
            dateTimeArray: obj.dateTimeArray,
            dateTimeArray1: obj1.dateTimeArray,
            dateTime1: obj1.dateTime,
            leader_id: options.leader_id || "",
            leader_type: options.leader_type || ""
        });
        this.getTmplIds();
    },
    changeDate(e) {
        this.setData({
            date: e.detail.value
        });
    },
    changeTime(e) {
        this.setData({
            time: e.detail.value
        });
    },
    changeDateTime(e) {
        const that = this;
        console.log("打印时间~~~~~~~~~~~~~~~~~~~~~", this.data.dateTimeArray);
        this.setData({
            dateTime: e.detail.value
        });
        console.log("打印时间", this.data.dateTime);
        var aaa1 = that.data.dateTime[0];
        var aaa2 = that.data.dateTime[1];
        var aaa3 = that.data.dateTime[2];
        var aaa4 = that.data.dateTime[3];
        var aaa5 = that.data.dateTime[4];
        var aaa6 = that.data.dateTime[5];
        var time1 = that.data.dateTimeArray[0][aaa1];
        var time2 = that.data.dateTimeArray[1][aaa2];
        var time3 = that.data.dateTimeArray[2][aaa3];
        var time4 = that.data.dateTimeArray[3][aaa4];
        var time5 = that.data.dateTimeArray[4][aaa5];
        var time6 = that.data.dateTimeArray[5][aaa6];
        var time = time1 + "-" + time2 + "-" + time3 + " " + time4 + ":" + time5 + ":" + time6;
        console.log("时间88888888888888888888888888888:", time);
    },
    changeDateTime1(e) {
        this.setData({
            dateTime1: e.detail.value
        });
    },
    changeDateTimeColumn(e) {
        var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;
        arr[e.detail.column] = e.detail.value;
        dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
        this.setData({
            dateTimeArray: dateArr,
            dateTime: arr
        });
    },
    changeDateTimeColumn1(e) {
        var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
        arr[e.detail.column] = e.detail.value;
        dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
        this.setData({
            dateTimeArray1: dateArr,
            dateTime1: arr
        });
    },
    //获取tmplIds
    getTmplIds: function() {
        let that = this;
        common.request({
            endpoint: "set/template_get",
            method: "post",
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                let tmplIds = [];
                let subscribeMessage = true;
                res.extra.map(v => {
                    if (v.type == "31") {
                        tmplIds.push(v.template_id);
                    }
                });
                if (!tmplIds.length) {
                    subscribeMessage = false;
                }
                that.setData({
                    tmplIds: tmplIds,
                    subscribeMessage: subscribeMessage
                });
            }
        });
    },
    confirm: function() {
        var _this = this;
        if (_this.data.subscribeMessage) {
            wx.requestSubscribeMessage({
                tmplIds: _this.data.tmplIds,
                success(res) {},
                fail(err) {
                    console.log(err);
                },
                complete() {
                    _this.confirm_ch();
                }
            });
        } else {
            _this.confirm_ch();
        }
    },
    confirm_ch: function() {
        var _this = this;
        common.request({
            endpoint: "Leader/pretrial/leader-rest",
            method: "post",
            data: {
                leader_id: _this.data.leader_id,
                est_time: _this.data.dateTimeArray[0][_this.data.dateTime[0]] + "-" + _this.data.dateTimeArray[1][_this.data.dateTime[1]] + "-" + _this.data.dateTimeArray[2][_this.data.dateTime[2]] + " " + _this.data.dateTimeArray[3][_this.data.dateTime[3]] + ":" + _this.data.dateTimeArray[4][_this.data.dateTime[4]] + ":" + _this.data.dateTimeArray[5][_this.data.dateTime[5]]
            },
            success: res => {
                wx.showToast({
                    icon: "none",
                    title: "操作成功"
                });
                let pages = getCurrentPages();
                let prevPage = pages[pages.length - 2];
                wx.navigateBack({
                    success() {
                        prevPage.getLeaderEntry();
                    }
                });
            },
            fail(res) {
                wx.showModal({
                    title: "提示",
                    content: res.message,
                    showCancel: false
                });
            }
        });
    }
});